package com.zsummer.zero.design.structure.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * jdk动态代理 
 * 必须要有接口
 * @author zhouyujiang
 *
 */
public class Demo2 {
	
	public static void main(String[] args) {
		Boy boy = new Boy();
		InvocationHandler handler = new BoyHander(boy);
		Buy boyProxy = (Buy)Proxy.newProxyInstance(Boy.class.getClassLoader(), Boy.class.getInterfaces(), handler);
		boyProxy.buyHouse();
		boyProxy.buyCar();
	}

}

interface Buy {
	void buyHouse();
	void buyCar();
}

class Boy implements Buy{

	@Override
	public void buyHouse() {
		System.out.println("Boy: pay house money");
		
	}

	@Override
	public void buyCar() {
		System.out.println("Boy: pay car money");
		
	}
	
}

class BoyHander implements InvocationHandler{

	private Object target;
	
	public BoyHander(Object target) {
		this.target = target;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("proxy: find resource");
		Object result = method.invoke(target, args);
		System.out.println("proxy: get proxy money");
		return result;
	}
	
}